package org.tis.tools.abf.module.ac.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.tis.tools.abf.module.ac.controller.request.AcRoleFuncAddRequest;
import org.tis.tools.abf.module.ac.controller.request.AcRoleFuncBatchAddRequest;
import org.tis.tools.abf.module.ac.controller.request.AcRoleFuncQueConditionRequest;
import org.tis.tools.abf.module.ac.controller.request.AcRoleFuncQueRequest;
import org.tis.tools.abf.module.ac.dao.AcRoleFuncMapper;
import org.tis.tools.abf.module.ac.entity.AcApp;
import org.tis.tools.abf.module.ac.entity.AcFunc;
import org.tis.tools.abf.module.ac.entity.AcRoleFunc;
import org.tis.tools.abf.module.ac.entity.enums.FuncType;
import org.tis.tools.abf.module.ac.entity.vo.AcAppRoleDetail;
import org.tis.tools.abf.module.ac.entity.vo.AcFuncExit;
import org.tis.tools.abf.module.ac.exception.AcExceptionCodes;
import org.tis.tools.abf.module.ac.exception.AcManagementException;
import org.tis.tools.abf.module.ac.exception.AcRoleFuncManagementException;
import org.tis.tools.abf.module.ac.service.IAcAppService;
import org.tis.tools.abf.module.ac.service.IAcFuncService;
import org.tis.tools.abf.module.ac.service.IAcRoleFuncService;
import org.tis.tools.core.utils.StringUtil;

import java.util.*;

import static org.tis.tools.core.utils.BasicUtil.wrap;

/**
 * acRoleFunc的Service接口实现类
 * 
 * @author Auto Generate Tools
 * @date 2018/04/23
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class AcRoleFuncServiceImpl extends ServiceImpl<AcRoleFuncMapper, AcRoleFunc> implements IAcRoleFuncService {

    @Autowired
    private IAcAppService iAcAppService;
    @Autowired
    private IAcFuncService iAcFuncService;
    @Autowired
    private IAcAppService acAppService;

    /**
     *通过角色ID查询某个角色的数据
     */
    @Override
    public AcRoleFunc queryRoleFunByCondition(AcRoleFunc acRoleFunc) throws AcRoleFuncManagementException {
        EntityWrapper<AcRoleFunc> acRoleFuncEntityWrapper = new EntityWrapper<>();
        if(StringUtils.isNotBlank(acRoleFunc.getGuidRole())){
            acRoleFuncEntityWrapper.eq(AcRoleFunc.COLUMN_GUID_ROLE,acRoleFunc.getGuidRole());
        }
        if(StringUtils.isNotBlank(acRoleFunc.getGuidApp())){
            acRoleFuncEntityWrapper.eq(AcRoleFunc.COLUMN_GUID_APP,acRoleFunc.getGuidApp());
        }
        if(StringUtils.isNotBlank(acRoleFunc.getGuidFunc())){
            acRoleFuncEntityWrapper.eq(AcRoleFunc.COLUMN_GUID_FUNC,acRoleFunc.getGuidFunc());
        }
        AcRoleFunc acRoleFunc1 = selectOne(acRoleFuncEntityWrapper);
        return acRoleFunc1;
    }

    /**
     *查询某个角色所有功能
     */
    @Override
    public List<AcRoleFunc> queryAllRoleFunByRoleGuid(String guidRole) throws AcRoleFuncManagementException {

        EntityWrapper<AcRoleFunc> acRoleFuncEntityWrapper = new EntityWrapper<>();
        acRoleFuncEntityWrapper.eq(AcRoleFunc.COLUMN_GUID_ROLE,guidRole);

        List<AcRoleFunc> acRoleFuncList = selectList(acRoleFuncEntityWrapper);
        return acRoleFuncList;
    }


    /**
     * 增加某个角色的功能
     */
    @Override
    public boolean addRoleFunc(AcRoleFuncAddRequest acRoleFuncAddRequest) throws AcRoleFuncManagementException {

        //判断该记录是否已经存在,如果存在则不需要重复添加
        Wrapper<AcRoleFunc> wrapper = new EntityWrapper<>();
        wrapper.eq(AcRoleFunc.COLUMN_GUID_ROLE,acRoleFuncAddRequest.getGuidRole());
        wrapper.eq(AcRoleFunc.COLUMN_GUID_FUNC,acRoleFuncAddRequest.getGuidFunc());
        wrapper.eq(AcRoleFunc.COLUMN_GUID_APP,acRoleFuncAddRequest.getGuidApp());

        AcRoleFunc acRoleFuncQurey = selectOne(wrapper);
        //该记录已经添加,不需要重复添加
        if (null != acRoleFuncQurey){
            throw new AcRoleFuncManagementException(AcExceptionCodes.FAILURE_WHRN_QUERY_IS_EXIST,wrap("该记录已存在",acRoleFuncAddRequest.getGuidFunc()));
        }
        //判断功能是否存在
        AcFunc acFunc = iAcFuncService.selectById(acRoleFuncAddRequest.getGuidFunc());
        if (null == acFunc){
            throw new AcRoleFuncManagementException(AcExceptionCodes.NOT_FUNC_WHEN_CREATE_ROLEFUNC,wrap("功能ID对应的功能不存在",acRoleFuncAddRequest.getGuidFunc()));
        }

        AcApp acApp = iAcAppService.selectById(acRoleFuncAddRequest.getGuidApp());
        if (null == acApp){
            throw new AcRoleFuncManagementException(AcExceptionCodes.NOT_APP_WHEN_CREATE_ROLEFUNC,wrap("应用ID对应的应用不存在",acRoleFuncAddRequest.getGuidApp()));
        }

        AcRoleFunc acRoleFunc = new AcRoleFunc();
        acRoleFunc.setGuidRole(acRoleFuncAddRequest.getGuidRole());
        acRoleFunc.setGuidFunc(acRoleFuncAddRequest.getGuidFunc());
        acRoleFunc.setGuidApp(acRoleFuncAddRequest.getGuidApp());

        boolean bolen = insert(acRoleFunc);
        return bolen;
    }
    /**
     * 修改某个角色的功能
     */
    @Override
    public AcRoleFunc update(AcRoleFuncAddRequest acRoleFuncAddRequest) throws AcRoleFuncManagementException {

        if (StringUtils.isNotBlank(acRoleFuncAddRequest.getGuid())){
            AcRoleFunc acRoleFunc = selectById(acRoleFuncAddRequest.getGuid());
            if (null == acRoleFunc){
                throw new AcRoleFuncManagementException(AcExceptionCodes.FAILURE_WHEN_QUERY_ROLE_FUNC,wrap("功能权限ID对应的权限集不存在"));
            }
        }else {
            throw new AcRoleFuncManagementException(AcExceptionCodes.FAILURE_WHEN_QUERY_ROLE_FUNC,wrap("权限集ID不能为空"));
        }
        //判断功能是否存在
        AcFunc acFunc = iAcFuncService.selectById(acRoleFuncAddRequest.getGuidFunc());
        if (null == acFunc){
            throw new AcRoleFuncManagementException(AcExceptionCodes.NOT_FUNC_WHEN_CREATE_ROLEFUNC,wrap("功能ID对应的功能不存在",acRoleFuncAddRequest.getGuidFunc()));
        }
        AcApp acApp = iAcAppService.selectById(acRoleFuncAddRequest.getGuidApp());
        if (null == acApp){
            throw new AcRoleFuncManagementException(AcExceptionCodes.NOT_APP_WHEN_CREATE_ROLEFUNC,wrap("应用ID对应的应用不存在",acRoleFuncAddRequest.getGuidApp()));
        }

        AcRoleFunc acRoleFunc1 = new AcRoleFunc();
        acRoleFunc1.setGuid(acRoleFuncAddRequest.getGuid());
        acRoleFunc1.setGuidRole(acRoleFuncAddRequest.getGuidRole());
        acRoleFunc1.setGuidFunc(acRoleFuncAddRequest.getGuidFunc());
        acRoleFunc1.setGuidApp(acRoleFuncAddRequest.getGuidApp());

        boolean bolen = updateById(acRoleFunc1);
        return acRoleFunc1;
    }




    /**
     * 删除某个角色的某些功能
     */
    @Override
    public boolean deleteAcRoleFuncByCondition(AcRoleFunc acRoleFunc) throws AcRoleFuncManagementException {
        EntityWrapper<AcRoleFunc> acRoleFuncEntityWrapper = new EntityWrapper<>();
        if(StringUtils.isNotBlank(acRoleFunc.getGuidRole())){
            acRoleFuncEntityWrapper.eq(AcRoleFunc.COLUMN_GUID_ROLE,acRoleFunc.getGuidRole());
        }
        if(StringUtils.isNotBlank(acRoleFunc.getGuidFunc())){
            acRoleFuncEntityWrapper.eq(AcRoleFunc.COLUMN_GUID_FUNC,acRoleFunc.getGuidFunc());
        }
        if(StringUtils.isNotBlank(acRoleFunc.getGuidApp())){
            acRoleFuncEntityWrapper.eq(AcRoleFunc.COLUMN_GUID_APP,acRoleFunc.getGuidApp());
        }
        boolean bolen = delete(acRoleFuncEntityWrapper);
        return bolen;
    }

    /**
     * 根据角色和应用id查询已有功能
     */
    @Override
    public List<AcFunc> queryFuncByRoleApp(String appId, String roleId, String funcId) throws AcRoleFuncManagementException {

        //根据条件查询记录
        Wrapper<AcRoleFunc> wrapper = new EntityWrapper<AcRoleFunc>();
        wrapper.eq(AcRoleFunc.COLUMN_GUID_APP,appId);
        wrapper.eq(AcRoleFunc.COLUMN_GUID_ROLE,roleId);

        List<AcRoleFunc> roleFuncList = selectList(wrapper);

        //根据查询结果查询所有的功能和行为
        List<AcFunc> funcList = new ArrayList<AcFunc>();
        for(AcRoleFunc acRoleFunc : roleFuncList){
            if (null != acRoleFunc){
                AcFunc acFunc = new AcFunc();

                Wrapper<AcFunc> funcWrapper = new EntityWrapper<AcFunc>();
                funcWrapper.eq(AcFunc.COLUMN_GUID,acRoleFunc.getGuidFunc());
                if (StringUtil.isEmpty(funcId)){
                    funcWrapper.isNull(AcFunc.COLUMN_GUID_FUNC);
                }else {
                    funcWrapper.eq(AcFunc.COLUMN_GUID_FUNC,funcId);
                }
                funcWrapper.eq(AcFunc.COLUMN_FUNC_TYPE, FuncType.FUNCTION);

                acFunc = iAcFuncService.selectOne(funcWrapper);

                funcList.add(acFunc);
            }
        }

        return funcList;
    }

    /**
     * 根据角色和应用id查询已有行为
     */
    @Override
    public List<AcFunc> queryBehaveByRoleApp(String appId, String roleId, String funcId) throws AcRoleFuncManagementException {

        //根据条件查询记录
        Wrapper<AcRoleFunc> wrapper = new EntityWrapper<AcRoleFunc>();
        wrapper.eq(AcRoleFunc.COLUMN_GUID_APP,appId);
        wrapper.eq(AcRoleFunc.COLUMN_GUID_ROLE,roleId);

        List<AcRoleFunc> roleFuncList = selectList(wrapper);

        //根据查询结果查询所有的功能和行为
        List<AcFunc> funcList = new ArrayList<AcFunc>();
        for(AcRoleFunc acRoleFunc : roleFuncList){
            if (null != acRoleFunc){
                AcFunc acFunc = new AcFunc();

                Wrapper<AcFunc> funcWrapper = new EntityWrapper<AcFunc>();
                funcWrapper.eq(AcFunc.COLUMN_GUID,acRoleFunc.getGuidFunc());
                if (StringUtil.isEmpty(funcId)){
                    throw new AcRoleFuncManagementException(AcExceptionCodes.PARMS_NOT_ALLOW_EMPTY,wrap("应用的id不能为空"));
                }else {
                    funcWrapper.eq(AcFunc.COLUMN_GUID_FUNC,funcId);
                }
                funcWrapper.eq(AcFunc.COLUMN_FUNC_TYPE, FuncType.BEHAVE);

                acFunc = iAcFuncService.selectOne(funcWrapper);

                funcList.add(acFunc);
            }
        }

        return funcList;
    }

    /**
     * 批量新增和删除角色的应用
     */
    @Override
    public void batchAdd(AcRoleFuncBatchAddRequest batchAddRequest) throws AcManagementException {

        //查询该角色下的已分配数据
        Wrapper<AcRoleFunc> allRoleFuncWrapper = new EntityWrapper<AcRoleFunc>();
        allRoleFuncWrapper.eq(AcRoleFunc.COLUMN_GUID_ROLE,batchAddRequest.getRoleId());
        List<AcRoleFunc> assignedList = selectList(allRoleFuncWrapper);

        //获取前端传来的数据
        List<AcRoleFunc> NotTreatedList = allNewList(batchAddRequest);

            List<AcRoleFunc> listNewAll = new ArrayList<AcRoleFunc>();
            //添加新数据
            if (null != NotTreatedList || 0 != NotTreatedList.size()){
                for (AcRoleFunc acRoleFunc : NotTreatedList){
                    if (null != acRoleFunc){
                        Wrapper<AcRoleFunc> funcAddWrapper = new EntityWrapper<AcRoleFunc>();
                        funcAddWrapper.eq(AcRoleFunc.COLUMN_GUID_ROLE,acRoleFunc.getGuidRole());
                        funcAddWrapper.eq(AcRoleFunc.COLUMN_GUID_APP,acRoleFunc.getGuidApp());
                        funcAddWrapper.eq(AcRoleFunc.COLUMN_GUID_FUNC,acRoleFunc.getGuidFunc());

                        AcRoleFunc acRoleFuncQue = selectOne(funcAddWrapper);
                        if (null == acRoleFuncQue){
                            AcRoleFunc acRoleFuncAdd = new AcRoleFunc();
                            acRoleFuncAdd.setGuidRole(acRoleFunc.getGuidRole());
                            acRoleFuncAdd.setGuidFunc(acRoleFunc.getGuidFunc());
                            acRoleFuncAdd.setGuidApp(acRoleFunc.getGuidApp());

                            insert(acRoleFuncAdd);
                            AcRoleFunc acRoleFunc1 = selectOne(funcAddWrapper);
                            listNewAll.add(acRoleFunc1);
                        }else {
                            listNewAll.add(acRoleFuncQue);
                        }
                    }
                }
            }

            //找出原数据有,现数据中没有的数据,用于删除使用
            assignedList.removeAll(listNewAll);
            //删除多于的数据
            if (null != assignedList){
                for (AcRoleFunc acRoleFunc : assignedList){
                    if (null != acRoleFunc){
                        deleteRoleFunc(acRoleFunc.getGuid());
                    }
                }
            }
    }

    /** 整理出前端传来的数据 */
    private List<AcRoleFunc> allNewList(AcRoleFuncBatchAddRequest batchAddRequest) {

        //处理前端传来的数据,存在list中
        List<AcRoleFunc> NotTreatedList = new ArrayList<AcRoleFunc>();
        if ( null != batchAddRequest.getBehaveIdList() || 0 != batchAddRequest.getBehaveIdList().size()){
            for (String behaveId : batchAddRequest.getBehaveIdList()){
                if (!StringUtil.isEmpty(behaveId)){
                    //查询该行为
                    AcFunc acBehave = iAcFuncService.selectById(behaveId);
                    if (null != acBehave){
                        //将该条数据的功能 存储在acRoleFunc对象中
                        AcRoleFunc acRoleFunc = new AcRoleFunc();
                        acRoleFunc.setGuidRole(batchAddRequest.getRoleId());
                        acRoleFunc.setGuidApp(acBehave.getGuidApp());
                        acRoleFunc.setGuidFunc(acBehave.getGuidFunc());

                        NotTreatedList.add(acRoleFunc);

                        //将该对象存储在acRoleBehave对象中
                        AcRoleFunc acRoleBehave = new AcRoleFunc();
                        acRoleBehave.setGuidRole(batchAddRequest.getRoleId());
                        acRoleBehave.setGuidApp(acBehave.getGuidApp());
                        acRoleBehave.setGuidFunc(behaveId);

                        NotTreatedList.add(acRoleBehave);
                    }
                }
            }
        }

        //去除list中重复的数据
        if (null != NotTreatedList || 0 != NotTreatedList.size()){
            HashSet<AcRoleFunc> hashSet = new HashSet<AcRoleFunc>(NotTreatedList);
            NotTreatedList.clear();
            NotTreatedList.addAll(hashSet);
        }

        return NotTreatedList;
    }

    /**
     * 查询所有应用和角色下已有应用
     */
    @Override
    public AcRoleFuncQueRequest<AcApp> queryAppByRole(String roleId) throws AcManagementException {

        //查询所有应用
        Wrapper<AcApp> appWrapper = new EntityWrapper<AcApp>();
        List<AcApp> allAppList = iAcAppService.selectList(appWrapper);

        //查询角色下已有应用
        List<AcApp> oldAppList = this.baseMapper.queryAppByRole(roleId);

        AcRoleFuncQueRequest<AcApp> queRequest = new AcRoleFuncQueRequest<AcApp>();
        queRequest.setAllData(allAppList);
        queRequest.setOldData(oldAppList);

        return queRequest;
    }

    /**
     * 通过角色和应用ID 查询该应用下的所有功能和行为,和已分配的功能和行为
     */
    @Override
    public List<AcFuncExit> queryFuncBehaveByRolApp(String appGuid, String roleGuid) throws
            AcManagementException {

        //返回数据
        List<AcFuncExit> exitList = new ArrayList<AcFuncExit>();

        //查询应用下的所有功能
        Wrapper<AcFunc> allFuncWrapper = new EntityWrapper<AcFunc>();
        allFuncWrapper.eq(AcFunc.COLUMN_GUID_APP,appGuid);
        allFuncWrapper.eq(AcFunc.COLUMN_FUNC_TYPE,FuncType.FUNCTION);
        List<AcFunc> allFuncList = iAcFuncService.selectList(allFuncWrapper);

        for (AcFunc acFunc : allFuncList){
            if (null != acFunc){
                //如果功能存在,获取功能下的所有行为
                Wrapper<AcFunc> allBehaveWrapper = new EntityWrapper<AcFunc>();
                allBehaveWrapper.eq(AcFunc.COLUMN_GUID_FUNC,acFunc.getGuid());
                allBehaveWrapper.eq(AcFunc.COLUMN_GUID_APP,appGuid);
                allBehaveWrapper.eq(AcFunc.COLUMN_FUNC_TYPE,FuncType.BEHAVE);
                List<AcFunc> allBehaveList = iAcFuncService.selectList(allBehaveWrapper);

                //查询功能下已分配的行为
                List<AcFunc> assignedBehaveList = this.baseMapper.queryBehaveByRole(roleGuid,appGuid,acFunc.getGuid());

                //如果该功能下行为非空,并且已分配的行为也非空,获取所有未分配的行为
                //boolean notBlank = ( 0 != allBehaveList.size()) && (0 != assignedBehaveList.size());
                if (0 != allBehaveList.size()){
                    allBehaveList.removeAll(assignedBehaveList);
                }

                //用于存放该功能下的所有行为,并标识出已分配的为"false",为分配的为"true"
                List<AcFuncExit> exitBehaveList = new ArrayList<AcFuncExit>();
                for (AcFunc behave: allBehaveList){
                    if (null != behave){
                        AcFuncExit acBehaveExit = new AcFuncExit(behave,"true",null,"Y",null);
                        exitBehaveList.add(acBehaveExit);
                    }
                }
                for (AcFunc behave: assignedBehaveList){
                    if (null != behave){
                        AcFuncExit acBehaveExit = new AcFuncExit(behave,"false",null,"Y",null);
                        exitBehaveList.add(acBehaveExit);
                    }
                }

                //处理完功能下的行为,处理该功能.判断该功能是否已分配
                Wrapper<AcRoleFunc> acRoleFuncWrapper = new EntityWrapper<AcRoleFunc>();
                acRoleFuncWrapper.eq(AcRoleFunc.COLUMN_GUID_ROLE,roleGuid);
                acRoleFuncWrapper.eq(AcRoleFunc.COLUMN_GUID_APP,appGuid);
                acRoleFuncWrapper.eq(AcRoleFunc.COLUMN_GUID_FUNC,acFunc.getGuid());

                AcRoleFunc acRoleFunc = selectOne(acRoleFuncWrapper);
                AcFuncExit acFuncExit = new AcFuncExit();
                if (null == acRoleFunc){
                    acFuncExit = new AcFuncExit(acFunc,"true",exitBehaveList,"Y", null);
                }
                if (null != acRoleFunc){
                    acFuncExit = new AcFuncExit(acFunc,"false",exitBehaveList,"Y",null);
                }

                exitList.add(acFuncExit);
            }
        }

        return exitList;
    }



    /**
     * 根据姓名通过角色和应用ID 查询该应用下的所有功能和行为,和已分配的功能和行为
     */
    @Override
    public List<AcFuncExit> queryFuncBehaveFilterByRolApp(AcRoleFuncQueConditionRequest conditionRequest) throws
            AcManagementException {

        //返回数据
        List<AcFuncExit> exitList = new ArrayList<AcFuncExit>();

        //查询应用下的所有功能
        Wrapper<AcFunc> allFuncWrapper = new EntityWrapper<AcFunc>();
        allFuncWrapper.eq(AcFunc.COLUMN_GUID_APP,conditionRequest.getAppId());
        allFuncWrapper.eq(AcFunc.COLUMN_FUNC_TYPE,FuncType.FUNCTION);
        List<AcFunc> allFuncList = iAcFuncService.selectList(allFuncWrapper);

        for (AcFunc acFunc : allFuncList){
            if (null != acFunc){
                //如果功能存在,获取功能下的所有行为
                Wrapper<AcFunc> allBehaveWrapper = new EntityWrapper<AcFunc>();
                allBehaveWrapper.eq(AcFunc.COLUMN_GUID_FUNC,acFunc.getGuid());
                allBehaveWrapper.eq(AcFunc.COLUMN_GUID_APP,conditionRequest.getAppId());
                allBehaveWrapper.eq(AcFunc.COLUMN_FUNC_TYPE,FuncType.BEHAVE);
                if (!StringUtil.isEmpty(conditionRequest.getName())){
                    allBehaveWrapper.like(AcFunc.COLUMN_FUNC_NAME,conditionRequest.getName());
                }
                List<AcFunc> allBehaveList = iAcFuncService.selectList(allBehaveWrapper);

                //查询功能下已分配的行为
                List<AcFunc> assignedBehaveList = new ArrayList<AcFunc>();
                if (StringUtil.isEmpty(conditionRequest.getName())){
                    assignedBehaveList = this.baseMapper.queryBehaveByRole(conditionRequest.getRoleId(),
                            conditionRequest.getAppId(),acFunc.getGuid());
                }else {
                    assignedBehaveList = this.baseMapper.queryBehaveFilterByRole(conditionRequest.getRoleId(),
                            conditionRequest.getAppId(),acFunc.getGuid(),conditionRequest.getName());
                }

                //如果该功能下行为非空,并且已分配的行为也非空,获取所有未分配的行为
                boolean notBlank = (0 != allBehaveList.size()) && ( 0 != assignedBehaveList.size());
                if (notBlank){
                    allBehaveList.removeAll(assignedBehaveList);
                }

                //用于存放该功能下的所有行为,并标识出已分配的为"false",为分配的为"true"
                List<AcFuncExit> exitBehaveList = new ArrayList<AcFuncExit>();
                if (0 != allBehaveList.size() || 0 != assignedBehaveList.size()) {
                    for (AcFunc behave : allBehaveList) {
                        if (null != behave) {
                            AcFuncExit acBehaveExit = new AcFuncExit(behave, "true", null,"Y",null);
                            exitBehaveList.add(acBehaveExit);
                        }
                    }
                    for (AcFunc behave : assignedBehaveList) {
                        if (null != behave) {
                            AcFuncExit acBehaveExit = new AcFuncExit(behave, "false", null,"Y",null);
                            exitBehaveList.add(acBehaveExit);
                        }
                    }
                }else {
                    //如果行为为空,并且功能也不能匹配,则不执行下面的操作
                    if (!StringUtil.isEmpty(conditionRequest.getName())){
                        Wrapper<AcFunc> acFuncWrapper = new EntityWrapper<AcFunc>();
                        acFuncWrapper.eq(AcFunc.COLUMN_GUID,acFunc.getGuid());
                        acFuncWrapper.like(AcFunc.COLUMN_FUNC_NAME,conditionRequest.getName());

                        AcFunc acFuncQue = iAcFuncService.selectOne(acFuncWrapper);
                        if (null == acFuncQue){
                            continue;
                        }
                    }
                }

                //处理完功能下的行为,处理该功能.判断该功能是否已分配
                Wrapper<AcRoleFunc> acRoleFuncWrapper = new EntityWrapper<AcRoleFunc>();
                acRoleFuncWrapper.eq(AcRoleFunc.COLUMN_GUID_ROLE,conditionRequest.getRoleId());
                acRoleFuncWrapper.eq(AcRoleFunc.COLUMN_GUID_APP,conditionRequest.getAppId());
                acRoleFuncWrapper.eq(AcRoleFunc.COLUMN_GUID_FUNC,acFunc.getGuid());

                AcRoleFunc acRoleFunc = selectOne(acRoleFuncWrapper);
                AcFuncExit acFuncExit = new AcFuncExit();
                if (null == acRoleFunc){
                    acFuncExit = new AcFuncExit(acFunc,"true",exitBehaveList,"Y",null);
                }
                if (null != acRoleFunc){
                    acFuncExit = new AcFuncExit(acFunc,"false",exitBehaveList,"Y",null);
                }

                exitList.add(acFuncExit);
            }
        }

        return exitList;
    }

    /**
     * 查询应用下的所有功能 和 角色和应用下的所有功能
     */
    @Override
    public AcRoleFuncQueRequest<AcFunc> queryFuncByRole(AcRoleFuncQueConditionRequest conditionRequest) throws AcManagementException {

        //判断应用id是否为空
        if (StringUtil.isEmpty(conditionRequest.getAppId())){
            throw new AcManagementException(AcExceptionCodes.APPID_ISNULL_WHRN_QUERY_ROLE_FUNC,wrap("应用的id不能为空"));
        }

        //查询应用下的所有功能
        Wrapper<AcFunc> funcWrapper = new EntityWrapper<AcFunc>();
        funcWrapper.eq(AcFunc.COLUMN_GUID_APP,conditionRequest.getAppId());
        funcWrapper.eq(AcFunc.COLUMN_FUNC_TYPE,FuncType.FUNCTION);

        List<AcFunc> allFuncs = iAcFuncService.selectList(funcWrapper);
        //查询角色和应用下的功能
        List<AcFunc> olFuncs = this.baseMapper.queryFuncByRole(conditionRequest.getRoleId(),conditionRequest.getAppId
                ());

        AcRoleFuncQueRequest<AcFunc> queRequest = new AcRoleFuncQueRequest<AcFunc>();
        queRequest.setAllData(allFuncs);
        queRequest.setOldData(olFuncs);

        return queRequest;
    }

    /**
     * 查询功能下的所有行为 和 角色和功能下的所有行为
     */
    @Override
    public AcRoleFuncQueRequest<AcFunc> queryBehaveByRole(AcRoleFuncQueConditionRequest conditionRequest) throws AcManagementException {

        //判断应用id是否为空
        if (StringUtil.isEmpty(conditionRequest.getAppId())){
            throw new AcManagementException(AcExceptionCodes.APPID_ISNULL_WHRN_QUERY_ROLE_FUNC,wrap("应用的id不能为空"));
        }
        //判断功能id是否为空
        if (StringUtil.isEmpty(conditionRequest.getFuncId())){
            throw new AcManagementException(AcExceptionCodes.FUNCID_ISNULL_WHRN_QUERY_ROLE_FUNC,wrap("功能的id不能为空"));
        }

        //查询功能下的所有行为
        Wrapper<AcFunc> funcWrapper = new EntityWrapper<AcFunc>();
        funcWrapper.eq(AcFunc.COLUMN_GUID_APP,conditionRequest.getAppId());
        funcWrapper.eq(AcFunc.COLUMN_GUID_FUNC,conditionRequest.getFuncId());
        funcWrapper.eq(AcFunc.COLUMN_FUNC_TYPE,FuncType.BEHAVE);

        List<AcFunc> allBehaves= iAcFuncService.selectList(funcWrapper);
        //查询功能下已有的行为
        List<AcFunc> oldBehaves = this.baseMapper.queryBehaveByRole(conditionRequest.getRoleId(),conditionRequest
                .getAppId(),conditionRequest.getFuncId());

        AcRoleFuncQueRequest<AcFunc> queRequest = new AcRoleFuncQueRequest<AcFunc>();
        queRequest.setOldData(oldBehaves);
        queRequest.setAllData(allBehaves);

        return queRequest;
    }

    /**
     * 硬删除权限集(角色)功能对应关系
     */
    @Override
    public void deleteRoleFunc(String id) throws AcManagementException {
        this.baseMapper.deleteRoleFunc(id);
    }

    /**
     * 过滤查询角色下功能行为
     *
     * @param conditionRequest
     */
    @Override
    public AcRoleFuncQueRequest<AcAppRoleDetail> getFilterFuncBehaveByRole(AcRoleFuncQueConditionRequest conditionRequest) throws AcManagementException {

        AcRoleFuncQueRequest<AcAppRoleDetail> acRoleFuncQueRequest = new AcRoleFuncQueRequest<>();

        if (StringUtil.isEmpty(conditionRequest.getName())){
            //查询出角色下所有的应用功能
            AcRoleFuncQueRequest<AcApp> acAppAcRoleFuncQueRequest = queryAppByRole(conditionRequest.getRoleId());
            List<AcAppRoleDetail> allData = getAppTree(acAppAcRoleFuncQueRequest.getAllData(),conditionRequest
                    .getRoleId());
            List<AcAppRoleDetail> oldData = getAppTree(acAppAcRoleFuncQueRequest.getOldData(),conditionRequest
                    .getRoleId());
            acRoleFuncQueRequest.setAllData(allData);
            acRoleFuncQueRequest.setOldData(oldData);
        }else {
            //模糊匹配出所有的功能
            List<AcFuncExit> funcExitList = new ArrayList<AcFuncExit>();

            //查询应用下的所有功能
            Wrapper<AcFunc> allFuncWrapper = new EntityWrapper<AcFunc>();
            allFuncWrapper.eq(AcFunc.COLUMN_FUNC_TYPE,FuncType.FUNCTION);
            allFuncWrapper.like(AcFunc.COLUMN_FUNC_NAME,conditionRequest.getName());
            List<AcFunc> allFuncList = iAcFuncService.selectList(allFuncWrapper);

            for (AcFunc acFunc : allFuncList){
                if (null != acFunc){
                    //如果功能存在,获取功能下的所有行为
                    Wrapper<AcFunc> allBehaveWrapper = new EntityWrapper<AcFunc>();
                    allBehaveWrapper.eq(AcFunc.COLUMN_GUID_FUNC,acFunc.getGuid());
                    allBehaveWrapper.eq(AcFunc.COLUMN_FUNC_TYPE,FuncType.BEHAVE);
                    List<AcFunc> allBehaveList = iAcFuncService.selectList(allBehaveWrapper);

                    //查询功能下已分配的行为
                    List<AcFunc> assignedBehaveList = this.baseMapper.queryBehaveByRoleFunc(conditionRequest.getRoleId(),acFunc.getGuid());

                    //如果该功能下行为非空,并且已分配的行为也非空,获取所有未分配的行为
                    if (0 != allBehaveList.size()){
                        allBehaveList.removeAll(assignedBehaveList);
                    }

                    //用于存放该功能下的所有行为,并标识出已分配的为"false",为分配的为"true"
                    List<AcFuncExit> exitBehaveList = new ArrayList<AcFuncExit>();
                    for (AcFunc behave: allBehaveList){
                        if (null != behave){
                            AcFuncExit acBehaveExit = new AcFuncExit(behave,"true",null,"Y",null);
                            exitBehaveList.add(acBehaveExit);
                        }
                    }
                    for (AcFunc behave: assignedBehaveList){
                        if (null != behave){
                            AcFuncExit acBehaveExit = new AcFuncExit(behave,"false",null,"Y",null);
                            exitBehaveList.add(acBehaveExit);
                        }
                    }

                    //处理完功能下的行为,处理该功能.判断该功能是否已分配
                    Wrapper<AcRoleFunc> acRoleFuncWrapper = new EntityWrapper<AcRoleFunc>();
                    acRoleFuncWrapper.eq(AcRoleFunc.COLUMN_GUID_ROLE,conditionRequest.getRoleId());
                    acRoleFuncWrapper.eq(AcRoleFunc.COLUMN_GUID_FUNC,acFunc.getGuid());

                    List<AcRoleFunc> roleFuncList = selectList(acRoleFuncWrapper);
                    AcFuncExit acFuncExit = new AcFuncExit();
                    if (0 == roleFuncList.size()){
                        acFuncExit = new AcFuncExit(acFunc,"true",exitBehaveList,"Y",null);
                    }
                    if (0 != roleFuncList.size()){
                        acFuncExit = new AcFuncExit(acFunc,"false",exitBehaveList,"Y",null);
                    }

                    funcExitList.add(acFuncExit);
                }
            }

            //将到guidApp相同的放在一起
            if (0 == funcExitList.size()){
                return null;
            }

            Map<String,List<AcFuncExit>> map = new HashMap<>();
            for (AcFuncExit acFuncExit : funcExitList){
                if (null != acFuncExit){
                    if (map.containsKey(acFuncExit.getGuidApp())){
                        map.get(acFuncExit.getGuidApp()).add(acFuncExit);
                    }else {
                        List<AcFuncExit> list = new ArrayList<AcFuncExit>();
                        list.add(acFuncExit);
                        map.put(acFuncExit.getGuidApp(),list);
                    }
                }
            }

            if (0 != map.size()){
                List<AcAppRoleDetail> allData = new ArrayList<AcAppRoleDetail>();
                for (String appGuid : map.keySet()){
                    AcApp acApp = acAppService.selectById(appGuid);
                    AcAppRoleDetail acAppRoleDetail = new AcAppRoleDetail(acApp,map.get(appGuid));
                    allData.add(acAppRoleDetail);
                }
                acRoleFuncQueRequest.setAllData(allData);
            }
        }
        return acRoleFuncQueRequest;
    }

    private List<AcAppRoleDetail> getAppTree(List<AcApp> list,String roleGuid){

        List<AcAppRoleDetail> appRoleDetailList = new ArrayList<AcAppRoleDetail>();

        if (0 == list.size()){
            return appRoleDetailList;
        }

        for (AcApp acApp : list){
            if (null != acApp){
                List<AcFuncExit> funcExitlist = queryFuncBehaveByRolApp(acApp.getGuid(),roleGuid);
                AcAppRoleDetail acAppRoleDetail = new AcAppRoleDetail(acApp,funcExitlist);
                appRoleDetailList.add(acAppRoleDetail);
            }
        }

        return appRoleDetailList;
    }

}

